package com.bocai.kelala.base;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bocai.kelala.R;
import com.bocai.kelala.util.ToastUitl;
import com.trello.rxlifecycle.components.support.RxFragment;

import butterknife.ButterKnife;

/***************
 * 使用例子
 *********************/
public abstract class BaseFragment extends RxFragment {
    protected View rootView;

    public Context mContext;
    private ProgressDialog mDialog;
    public Toolbar mToolbar;
    public TextView mTitleTv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null)
            rootView = inflater.inflate(getLayoutResource(), container, false);

        ButterKnife.bind(this, rootView);
        mContext = getActivity();

        initPresenter();
        initView();


        return rootView;
    }

    //获取布局文件
    protected abstract int getLayoutResource();

    //简单页面无需mvp就不用管此方法即可,完美兼容各种实际场景的变通
    public abstract void initPresenter();

    //初始化view
    protected abstract void initView();


    /**
     * 通过Class跳转界面
     **/
    public void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(cls, null, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    protected void initToolbar(String title) {
        mToolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        mTitleTv = (TextView) rootView.findViewById(R.id.tv_title);
        mTitleTv.setText(title);
    }

    protected void initBack(ImageView img) {
        img.setImageResource(R.mipmap.back);
        img.setVisibility(View.VISIBLE);
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
    }
    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }


    /**
     * 开启加载进度条
     */
    public void startProgressDialog() {
        if (mDialog == null) {
            mDialog = new ProgressDialog(getActivity());
            mDialog.setCanceledOnTouchOutside(false);
            mDialog.setMessage("加载中...");
        }
        if (!mDialog.isShowing())
            mDialog.show();
    }

    /**
     * 开启加载进度条
     *
     * @param msg
     */
    public void startProgressDialog(String msg) {
        if (mDialog == null) {
            mDialog = new ProgressDialog(getActivity());
            mDialog.setCanceledOnTouchOutside(false);
            mDialog.setMessage("加载中...");
        }
        if (!mDialog.isShowing())
            mDialog.show();
    }

    /**
     * 停止加载进度条
     */
    public void stopProgressDialog() {
        if (mDialog != null)
            if (mDialog.isShowing())
                mDialog.dismiss();
    }


    /**
     * 短暂显示Toast提示(来自String)
     **/
    public void showShortToast(String text) {
        ToastUitl.showShort(text);
    }

    /**
     * 短暂显示Toast提示(id)
     **/
    public void showShortToast(int resId) {
        ToastUitl.showShort(resId);
    }

    /**
     * 长时间显示Toast提示(来自res)
     **/
    public void showLongToast(int resId) {
        ToastUitl.showLong(resId);
    }

    /**
     * 长时间显示Toast提示(来自String)
     **/
    public void showLongToast(String text) {
        ToastUitl.showLong(text);
    }

//
//    public void showToastWithImg(String text, int res) {
//        ToastUitl.showToastWithImg(text,res);
//    }
//
//    /**
//     * 网络访问错误提醒
//     */
//    public void showNetErrorTip() {
//        ToastUitl.showToastWithImg(getText(R.string.net_error).toString(),R.drawable.ic_wifi_off);
//    }
//
//    public void showNetErrorTip(String error) {
//        ToastUitl.showToastWithImg(error,R.drawable.ic_wifi_off);
//    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


}
